package com.yootk.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(Exception.class)  // 可以捕获的异常类型
    @ResponseBody
    public Object exceptionHandler(Exception e) {   // 实现所有的异常处理
        Map<String, Object> map = new HashMap<>();  // 保存响应信息
        map.put("message", e.getMessage());     // 直接获取异常信息
        map.put("status", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);    // 设置一个HTTP状态码
        map.put("exception", e.getClass().getName());   // 获取异常类型
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        map.put("path", request.getRequestURI());       // 异常发生的路径
        map.put("content", "服务端程序出错。");
        return map;
    }
}
